{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "pythonjvsc74a57bd08ab0d968002a578cf1e1aa041721a175249ba338f6da29efa5ef1a380c630376",
   "display_name": "Python 3.8.5 64-bit (conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 列表、元组、集合、字典与for循环"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "## 1.列表 `[ ]`\n",
    "python中的列表相当于是c语言中的数组，不同点在于列表支持python所有的数据类型\n",
    "## 1.1利用列表实现`队列`和`栈`:\n",
    "`队列`：   \n",
    "利用`insert(位置,数据)`函数并始终将第一个参数设置为0，每次都从列表的第一个位置插入数据。    \n",
    "利用`pop()`函数将列表的最后一个元素弹出    \n",
    "`栈`：   \n",
    "利用`append()`函数向队列末尾追加元素，    \n",
    "利用`pop()`函数弹出队列末尾元素"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义一个空列表，当作队列\n",
    "queue = []\n",
    "#插入元素\n",
    "queue.insert(0,1)\n",
    "queue.insert(0,2)\n",
    "queue.insert(0,3)\n",
    "print(queue)\n",
    "#取出元素\n",
    "print(queue.pop())\n",
    "print(queue.pop())\n",
    "print(queue.pop())\n",
    "#定义一个空列表，当作栈\n",
    "stack = []\n",
    "#追加元素\n",
    "stack.append(1)\n",
    "stack.append(2)\n",
    "stack.append(3)\n",
    "print(stack)\n",
    "#弹出元素\n",
    "print(stack.pop())\n",
    "print(stack.pop())\n",
    "print(stack.pop())\n"
   ]
  },
  {
   "source": [
    "## 2.元组 `( )`  \n",
    "元组就是不可变列表，一旦创建完成就无法修改"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#创建元组\n",
    "tuplename = (\"xiaohong\",\"xiaohua\",\"xiaoxi\")\n",
    "print(tuplename)"
   ]
  },
  {
   "source": [
    "## 3.字典 `{ }`     \n",
    "不同于列表与元组是有序的，字典是无序的，访问字典的值是通过与之对应的唯一的键进行的。\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#创建字典\n",
    "a = {\"name\":\"xiaoke\",\"age\":18,\"score\":100}\n",
    "print(a)\n",
    "#添加键值对\n",
    "a[\"label\"] = \"freedom\"\n",
    "print(a)"
   ]
  },
  {
   "source": [
    "## 4.集合 `{ }`\n",
    "python中集合的概念与数学中是一致的，且集合中只能存储不可变的数据类型如`整型`、`浮点型`、`字符串`、`元组`，不能存储`列表`、`集合`、`字典`\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "{1, 2, 2.1}\n{3, 1, 2, 2.1}\n"
     ]
    }
   ],
   "source": [
    "#创建集合\n",
    "a = {1,2,2.1,1}\n",
    "print(a)\n",
    "#往集合中添加元素:add()函数\n",
    "a.add(3)\n",
    "print(a)"
   ]
  },
  {
   "source": [
    "## 5.for循环       \n",
    "`for` 迭代变量 `in`  `字符串`|`列表`|`元组`|`字典`|`集合`     \n",
    "----代码块\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}